package com.jmb.api.web.controller.data.factory;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.result.Result;
import com.jmb.api.entity.DataFactory;
import com.jmb.api.vo.DataFactory.DataFactoryInfoVO;
import com.jmb.api.vo.DataFactory.DataFactoryQueryVO;
import com.jmb.api.vo.DataFactory.DataFactoryVO;
import com.jmb.api.web.service.DataFactoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 数据工厂
 */

@Tag(name = "数据工厂")
@RestController
@RequestMapping("/web/dataFactory")
public class DataFactoryController {

    @Autowired
    private DataFactoryService dataFactoryService;

    /**
     * 新增更新数据源中心
     */
    @Operation(summary = "新增更新数据源中心")
    @PostMapping("/saveOrUpdateDataFactory")
    public Result saveOrUpdateDataFactory(@RequestBody @Valid DataFactory dataFactory){
        dataFactoryService.saveOrUpdateDataFactory(dataFactory);
        return Result.ok();
    }

    /**
     * 查询数据源中心
     */
    @Operation(summary = "查询数据源中心")
    @GetMapping("/getDataFactory/{id}")
    public Result<DataFactory> getDataFactory(@PathVariable Long id){
        DataFactory dataFactory = dataFactoryService.getById(id);
        return Result.ok(dataFactory);
    }

    /**
     * 删除数据源中心
     */
    @Operation(summary = "删除数据源中心")
    @GetMapping("/deleteDataFactory/{id}")
    public Result deleteDataFactory(@PathVariable Long id){
        dataFactoryService.removeById(id);
        return Result.ok();
    }

    // 执行数据工厂
    @Operation(summary = "执行数据工厂")
    @GetMapping("/executeDataFactory/{id}")
    public Result executeDataFactory(@PathVariable Long id){
        // 执行数据工厂
        Long runTime  = dataFactoryService.executeDataFactory(id);
        return Result.ok(runTime);
    }

    // 数据工厂列表
    @Operation(summary = "数据工厂列表")
    @GetMapping("/listDataFactory")
    public Result<IPage<DataFactoryInfoVO>> listDataFactory(DataFactoryQueryVO dataFactoryQueryVO){
        // 数据工厂列表
        IPage<DataFactoryInfoVO> page = new Page<>(dataFactoryQueryVO.getPageNum(),
                dataFactoryQueryVO.getPageSize());
        // 查询数据
        IPage<DataFactoryInfoVO> list = dataFactoryService.listDataFactory(page, dataFactoryQueryVO);
      return Result.ok(list);
    }


}
